/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {

    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    $('.btn-save').on('click', function(){
        $('.file-content').val(ue.getContent());
        var data = {
            title: $('.file-title').val(),
            type: $('.file-type').val(),
            score: $('.file-score').val(),
            content: $('.file-content').val(),
            role: $('.file-user-role').val()
        };

        if (data.role == undefined) {
            data.role = 0;
        }

        if (checkForm(data)) {
            saveData('/v1/manager/save/score/file', {params:data}, '/manager/file.html');
        }
    });

    $('.file-type').on('change', function () {
        if ($(this).val() == '1') {
            $(this).parents('.form-group').parent().append('<div class="form-group form-user-role"><label for="inputEmail3" class="col-sm-2 control-label">角色</label><div class="col-sm-6"><select class="form-control file-user-role"><option value="">请选择角色</option><option value="1">教师</option><option value="5">学生</option></select></div></div>');
        } else {
            $('.form-user-role').remove();
        }
    });
});

function checkForm(data) {
    if (data.title == '') {
        alertTip('标题不能为空');
        return false;
    }
    if (data.score == '') {
        alertTip('分数不能为空');
        return false;
    }
    if (data.type == '') {
        alertTip('请选择类型');
        return false;
    }
    if (data.content == '') {
        alertTip('内容不能为空');
        return false;
    }
    if (data.type == '1') {
        if (data.role != '1' && data.role != '5') {
            alertTip('请选择角色');
            return false;
        }
    }
    return true;
}